﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;

namespace Basteroid
{
    class GL_DataBase
    {
        public enum GL_DB_ColumnType
        {
            GLDB_CT_Int,
            GLDB_CT_Chars
        };

        private string m_str_ConnectionString = "";

        public GL_DataBase(string connectionString)
        {
            m_str_ConnectionString = connectionString;
        }

        public SqlConnection GL_CreateConnection(string connectionString)
        {
            return new SqlConnection(connectionString);
        }

        public string GL_SelectFrom(string str_Col_Aim, string str_Table, string str_Col_Source, string str_Rcrd_Source)
        {
            try
            {
                SqlConnection sql_Connection = GL_CreateConnection(m_str_ConnectionString);
                sql_Connection.Open();

                SqlCommand sql_Command = new SqlCommand(
                    "select " + str_Col_Aim +
                    " from " + str_Table +
                    " where " + str_Col_Source +
                    " = '" + str_Rcrd_Source + "'");

                sql_Command.Connection = sql_Connection;
                SqlDataReader sql_DataRdr = sql_Command.ExecuteReader();

                if (sql_DataRdr.Read())
                {
                    string str_Rcrd_Aim = sql_DataRdr[0].ToString();
                    sql_Connection.Close();
                    return str_Rcrd_Aim;
                }
                else
                {
                    //MessageBox.Show("Reading "+str_Col_Aim+" error!");
                    sql_Connection.Close();
                    return "";
                }
            }
            catch
            {
                return "";
            }
        }

        public int GL_InsertInto(string str_Tablename, string str_Col_Aim, string str_AimValues)
        {
            try
            {
                SqlConnection sql_Connection = GL_CreateConnection(m_str_ConnectionString);
                sql_Connection.Open();

                SqlCommand sql_Command = new SqlCommand(
                    " insert into " + str_Tablename + " ( " + str_Col_Aim + " ) "
                    + "values ( " + str_AimValues + " ) ");
                sql_Command.Connection = sql_Connection;
                Console.WriteLine(" insert into " + str_Tablename + " ( " + str_Col_Aim + " ) "
                    + "values ( " + str_AimValues + " ) ");
                int i_LinesEffected = Convert.ToInt32(sql_Command.ExecuteNonQuery());
                sql_Connection.Close();
                Console.WriteLine("i_LinesEffected: " + i_LinesEffected.ToString());
                return i_LinesEffected;
            }
            catch
            {
                return 0;
            }
        }

        public int GL_UpdateInto(string str_Tablename,
            string str_Col_Aim, string str_AimValues,
            string str_Col_Source, string str_Rcrd_Source)
        {
            try
            {
                SqlConnection sql_Connection = GL_CreateConnection(m_str_ConnectionString);
                sql_Connection.Open();

                SqlCommand sql_Command = new SqlCommand(
                    "update " + str_Tablename + " set "
                    + str_Col_Aim + " = '" + str_AimValues
                    + "' where " + str_Col_Source + " = " + str_Rcrd_Source);
                sql_Command.Connection = sql_Connection;

                int i_LinesEffected = Convert.ToInt32(sql_Command.ExecuteNonQuery());
                sql_Connection.Close();
                return i_LinesEffected;
            }
            catch
            {
                return 0;
            }
        }
    }
}
